package com.yy.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mongodb.client.model.UpdateManyModel;
import com.yy.dao.TestUserDao;
import com.yy.pojo.Article;
import com.yy.pojo.TestUser;
import com.yy.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author young
 * @date 2022/8/17 11:19
 * @description: service实现类
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public boolean insertArticle(Article article) {
        try{
        article.setTimer(LocalDateTime.now());
        mongoTemplate.insert(article);
        return true; }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public int updateArticle(String ids,String titles) {
        //通过query根据id查询出对应对象，通过update对象进行修改
        Query id = new Query(Criteria.where("_id").is(ids));
        Update title = new Update().set("title", titles);
        try {
            mongoTemplate.updateFirst(id,title,Article.class);
            log.info("更新成功！");
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            log.info("更新失败！");
            return 0;
        }
    }

    @Override
    public boolean removeArticle(String id) {
        Query id1 = new Query(Criteria.where("_id").is(id));
        try {
            mongoTemplate.remove(id1,Article.class);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        
    }

    @Override
    public Article findOne(String title) {
        Query titles = new Query(Criteria.where("title").is(title));
        return mongoTemplate.findOne(titles, Article.class);
       
    }

   

    @Override
    public List<Article> findLike(Article article) {
        Pattern pattern = Pattern.compile("^.*" + article.getTitle().trim() + ".*$", Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("title").regex(pattern));
        return  mongoTemplate.find(query,Article.class);
    }

    @Override
    public List<Article> findMore(Article article) {
        Query query = new Query(Criteria.where("title").is(article.getTitle()));
        return mongoTemplate.find(query,Article.class);
    }

    @Override
    public List<Article> all() {
        return mongoTemplate.findAll(Article.class);
    }

    @Override
    public List<Article> findTimes(Article article) {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC,"timer"));
        return mongoTemplate.find(query,Article.class);
    }

    @Override
    public List<Article> findTimeByPage(Article article) {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC,"timer"));
        query.skip(0).limit(2);
        return mongoTemplate.find(query,Article.class);
    }


}
